//document.write("<script language='javascript' src=ezca-XTXApp.js></script>");
//初始化常量
var KEY_SIGNOREXCHANGE = 0;
var KEY_SIGNATURE= 1;
var KEY_KEYEXCHANGE= 2;
var CERT_NOT_BEFORE= 17;
var CERT_NOT_AFTER = 18;


//检查客户端CA环境是否正常
try {

		if (window.ActiveXObject) {		
			document.writeln("<OBJECT classid=\"CLSID:C538AA4C-73A8-4069-B466-DF2D71FDCDAF\" height=1 id=XTXAPP style=\"HEIGHT: 1px; LEFT: 10px; TOP: 28px; WIDTH: 1px\" width=1 VIEWASTEXT>");	
			document.writeln("</OBJECT>");	
		
				XTXAPP.SOF_GetVersion();	
				}
				 else 
				 	{		
				 		document.writeln("<embed id=XTXAPP0 type=application/x-xtx-axhost clsid={C538AA4C-73A8-4069-B466-DF2D71FDCDAF} event_OnUsbkeyChange=OnUsbKeyChange width=1 height=1 />");	
				 			XTXAPP = document.getElementById("XTXAPP0");		
				 			XTXAPP.SOF_GetVersion();	
				 	
				 			}}
				 			catch(e)
				 			 {
				 			     alert("证书应用环境错误，请下载驱动安装！");
				 			     window.open('http://www.ezca.org/ezcaWeb/download.jsp?id=e52t4ho46lsz7h5b689u', 'newwindow', 'height=0, width=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');
				 			 	
				 			 	}
				 			
				 			
/**
 * 客户端函数
 */ 		
				 			
//获取证书列表
	function GetUserList(strListID)
	{	

		var objListID = eval(strListID);
			var strUserList = SOF_GetUserList();	
				
			var m=1;	
			while (1) {	
				
					var i = strUserList.indexOf("&&&");		
				  var j = strUserList.indexOf("||");	
					if (i <= 0 & j<=0) {			break;		}	
					if (m<=0) {			break;		}	
					if(i>0)	{
				var strOneUser = strUserList.substring(0, i);	}
				
				if(j>0 & i<=0)	{
				var strOneUser = strUserList;	
				m=0;
				}
				
					var strName = strOneUser.substring(0, strOneUser.indexOf("||"));	
					var strUniqueID = strOneUser.substring(strOneUser.indexOf("||") + 2, strOneUser.length);		
					var objItem = new Option(strName, strUniqueID);		
					objListID.options.add(objItem);		
					
					var len = strUserList.length;		
					strUserList = strUserList.substring(i + 3,len);	
					
					}	
					var objListID = null;
						return;
						}
	//弹出窗口获取证书列表
	function GetUserListWindow(strListID)
{
	//var objListID = eval(strListID);
	var strUserList = SOF_GetUserList();
	var i = strUserList.indexOf("&&&");
	var j = strUserList.indexOf("||");
	if(i <= 0 & j<=0)
	{
	}
	if(j>0 & i<=0)
	{
		var strOneUser = strUserList;	
		var strName = strOneUser.substring(0, strOneUser.indexOf("||"));	
		var strUniqueID = strOneUser.substring(strOneUser.indexOf("||") + 2, strOneUser.length);
		document.getElementById("UserList").value=strUniqueID;
	}
	if(j>0& i>0)
	{
			var temp=window.showModalDialog('../UserList.aspx',strUserList,'dialogHeight:150px;center: yes;location:0; dialogWidth:260px;status:no;help:no;location=no');
			document.getElementById("UserList").value=temp;
	}
}
	
//从证书列表删除
function RemoveUserList(strListID){	
	var objListID = eval(strListID);	
	var i;	
	var n = objListID.length;
		for(i = 0; i < n; i++) {	
				objListID.remove(0);	
				}}
//修改证书列表
function ChangeUserList(strListID)
{	
	RemoveUserList(strListID);	
	GetUserList(strListID);}

//拔key重新获取证书列表
function OnUsbKeyChange(){
		ChangeUserList("LoginForm.UserList");}

//获取证书列表
function SOF_GetUserList(){	
return XTXAPP.SOF_GetUserList();}

//导出证书信息
function SOF_ExportUserCert(CertID, certType){	
	if (certType == KEY_SIGNOREXCHANGE) {	
			return XTXAPP.SOF_ExportUserCert(CertID);	}
			 else if (certType == KEY_SIGNATURE) {	
			 		var signCert = XTXAPP.SOF_ExportUserCert(CertID);	
			 		var encCert = XTXAPP.SOF_ExportExChangeUserCert(CertID);	
	if (signCert != encCert)
	 {			return signCert;		} 
	 else { /* 不存在签名证书 */			return "";		}	} 
	 else if (certType == KEY_KEYEXCHANGE) {
	     return XTXAPP.SOF_ExportExChangeUserCert(CertID);
        	} 
	 	else {		return "";	}}

//校验Key密码
function SOF_Login(CertID, passwd){	
	var ret = XTXAPP.SOF_Login(CertID, passwd);	
	if (ret) {		return true;	} 
	else 
		{		return false;	}}

//修改key密码
function SOF_ChangePassWd(CertID, OldPassWd, NewPassWd){	
	var ret = XTXAPP.SOF_ChangePassWd(CertID, OldPassWd, NewPassWd);	
	if (ret) {		return true;	} 
	else {		return false;	}}
//获取证书信息	   	
function SOF_GetCertInfo(cert, typ){		return XTXAPP.SOF_GetCertInfo(cert, typ);}	

//P1签名
function SOF_SignData(CertID, inData){	return XTXAPP.SOF_SignData(CertID, inData);}
//P7签名
function SOF_SignDataByP7(CertID, inData){	return XTXAPP.SOF_SignDataByP7(CertID, inData);}

//文件签名
function SOF_SignFile(CertID, inFile){
		return XTXAPP.SOF_SignFile(CertID, inFile);}

//加密数据
function SOF_EncryptData(sCert, Indata){
		return XTXAPP.SOF_EncryptData(sCert, Indata);}
function SOF_EncryptDataEx(sCert, Indata){	
	return XTXAPP.SOF_EncryptDataEx(sCert, Indata);}

//解密数据
function SOF_DecryptData(CertID, Indata){	
	return XTXAPP.SOF_DecryptData(CertID, Indata);}



//文件加密
function SOF_SymDecryptFile(sKey, InFile, OutFile){	
	var ret = XTXAPP.SOF_SymDecryptFile(sKey, InFile, OutFile);
		if (ret) {		return true;	} 
		else 
			{		return false;	}}

//非对称加密
function SOF_PubKeyEncrypt(Cert, InData){
		return XTXAPP.SOF_PubKeyEncrypt(Cert, InData);}

//非对称解密
function SOF_PriKeyDecrypt(CertID, InData){	
	return XTXAPP.SOF_PriKeyDecrypt(CertID, InData);}

//文件取hash值
function SOF_HashFile(hashAlg, InFile){	
	return XTXAPP.SOF_HashFile(hashAlg, InFile);}

//检查证书有效性
function CheckValid(userCert){
		var strNotBefore = SOF_GetCertInfo(userCert, CERT_NOT_BEFORE);	
		var strNotBefore_year = strNotBefore.substring(0, 4);	
		var strNotBefore_month = strNotBefore.substring(5, 7);	
		var strNotBefore_day = strNotBefore.substring(8, 10);
		var notBeforeDate = strNotBefore_year + "/" + strNotBefore_month + "/" + strNotBefore_day;	
    var nowDate = new Date().Format("yyyy/MM/dd");
		var days = (Date.parse(notBeforeDate) - Date.parse(nowDate)) / (1000*60*60*24);	if (days > 0) {	
				alert("您的证书尚未生效!距离生效日期还剩" + days + "天!");		
				return false;	}	
		var strNotAfter = SOF_GetCertInfo(userCert, CERT_NOT_AFTER);
		var strNotAfter_year = strNotAfter.substring(0, 4);
		var strNotAfter_month = strNotAfter.substring(5, 7);	
		var strNotAfter_day = strNotAfter.substring(8, 10);	
		var notAfterDate = strNotAfter_year + "/" + strNotAfter_month + "/" + strNotAfter_day;
		var nowDate = new Date().Format("yyyy/MM/dd");	days = (Date.parse(notAfterDate) - Date.parse(nowDate)) / (1000*60*60*24);
		
		if (days <= -45) {		
			alert("您的证书已过期 "+ -days +" 天，超过了最后使用期限！请更新证书!");
			window.open("http://www.ezca.org/ezcaWeb/servicescertificatesupdate.jsp");
			
		return false;	}	
		if (days >= 0 && days <= 60) {	
				alert("您的证书还有" + days + "天过期，请更新证书!");
				window.open("http://www.ezca.org/ezcaWeb/servicescertificatesupdate.jsp");	
				return true;	}
		if (days < 0) {	
			alert("您的证书已过期 "+ -days +" 天，请更新证书!");
			//alert("您的证书已过期 "+ -days +" 天，请根据提示自助更新!");
			window.open("http://www.ezca.org/ezcaWeb/servicescertificatesupdate.jsp");		
		}
		  	
		return true;}
				
 //身份认证 
 function Login(strFormName, strCertID){	
	var objForm = eval(strFormName);	
	if (objForm == null) {		
		alert("表单错误");		
		return false;	}	
		if (strCertID == null || strCertID == "")
		{ alert("未找到证书,请插入USBKEY!"); return false; }
			
				var userCert = SOF_ExportUserCert(strCertID, KEY_SIGNOREXCHANGE);
					if (userCert == null || userCert == "") {
					    alert("未找到证书,请插入USBKEY!");		
							return false;	}	
					
				if (!CheckValid(userCert)) {		return false;	}
			
			var strClientSignedData = SOF_SignDataByP7(strCertID, strServerRan);	
       		
       		if (strClientSignedData == null || strClientSignedData == "") 
       		{		alert("客户端签名失败!");		return false;	}	
       		var strSignItem = "<input type=\"hidden\" name=\"UserSignedData\" value=\"\">";
       			if (objForm.UserSignedData == null) {	
       					objForm.insertAdjacentHTML("BeforeEnd", strSignItem);	}	
       				
       		var strContainerItem = "<input type=\"hidden\" name=\"ContainerName\" value=\"\">";
       			if (objForm.ContainerName == null) {		
       					objForm.insertAdjacentHTML("BeforeEnd", strContainerItem);	}
       					objForm.id.value = strClientSignedData;
       					objForm.ContainerName.value = strCertID;
       					objForm.action = "../WebSite/CheckLogin.aspx";
       					return true;
       }
//文件签名
 function SignFile(strCertID,data){	
//	var objForm = eval(strFormName);	
//	if (objForm == null) {		
//		alert("表单错误");		
//		return false;	}	
		if (strCertID == null || strCertID == "")
		{ alert("未找到证书,请插入USBKEY!"); return false; }
		
		//检查证书有效性
				var userCert = SOF_ExportUserCert(strCertID, KEY_SIGNOREXCHANGE);
					if (userCert == null || userCert == "") {
					    alert("未找到证书,请插入USBKEY!");		
							return false;	}	
					
				if (!CheckValid(userCert)) {		return false;	}
			
				//获取客户端数据
			var initdata = data;
			var signdata=SOF_SignData(strCertID,data);
			var sn = SOF_GetCertInfo(SOF_ExportUserCert(strCertID, KEY_SIGNOREXCHANGE), 2);
       		if (initdata == null || signdata == "") 
       		{		alert("客户端签名失败!");		return false;	}
       		
       		document.getElementById("UserSignedData").value = signdata;
       		document.getElementById("initdata").value = data;
       		document.getElementById("sn").value = sn;
       }